﻿using PerpheralDevice.IRepository;
using PerpheralDevice.Units;
using System.Serialization;

namespace AppMachine.Domain.LogicCom.AppComs;

/// <summary>
///  应用组件(输入开关组件)
/// </summary>
public class ComSwitchRO : ComSwitch {

    public ComSwitchROParam ComSwitchROParam {get; set;}
    public override bool IsDisabled => this.ComSwitchROParam.IsDisbaled;
    public override bool IsReversed => this.ComSwitchROParam.IsReversed;
    public bool IsAlarmDI => this.ComSwitchROParam.IsAlarmDI;
    public int MockID => this.ComSwitchROParam.MockID;

    public ComSwitchRO(IPerpDevRepository perpDevRepository, IComConfig comConfig, IComRepository comRepository)
        : base(perpDevRepository, comConfig, comRepository) {

        this.ComSwitchROParam = YamlExtend.GetObject<ComSwitchROParam>(this.ComConfig.ComParamSerialized) ?? new();

        SwitchRO tempSwitchRO = this.PerpDev.Find<SwitchRO>(this.PerpUnitID);
        tempSwitchRO?.MockIDSet(this.ComSwitchROParam.MockID);
        tempSwitchRO?.Add<SwitchRO>((tempSwitchRO) => { this.OnStatusChangedRaise(); });
    }

    public override bool? IsOpen=>this.PerpDev.Find<SwitchRO>(this.ComConfig.PerpSeatID!)?.View().IsOpen;

    public ComSwitchRO View() {
        this.PerpDev.Find<SwitchRO>(this.PerpUnitID).View();
        return this;
    }

    public ComSwitchRO MockIDSet(int mockID) {
        this.ComSwitchROParam.MockID = mockID;
        this.PerpDev.Find<SwitchRO>(this.PerpUnitID)?.MockIDSet(mockID);
        return this;
    }
    public ComSwitchRO IsDisabledSet(bool isDisabled) {
        this.ComSwitchROParam.IsDisbaled = isDisabled;
        return this;
    }
    public ComSwitchRO IsReversedSet(bool isReversed) {
        this.ComSwitchROParam.IsReversed = isReversed;
        return this;
    }
    public ComSwitchRO IsAlarmDISet(bool isAlarmDI) {
        this.ComSwitchROParam.IsAlarmDI = isAlarmDI;
        return this;
    }
    public override AppCom Save() {
        (this.ComConfig as AppComConfig)!.ComParamSerialized = YamlExtend.GetString(this.ComSwitchROParam);
        return base.Save();
    }

}
